glance-pkg:
  pkg.installed:
    - names:
      - openstack-glance

glance-api-conf:
  file.managed:
    - name: /etc/glance/glance-api.conf
    - source: salt://openstack/queen/glance/files/glance-api.conf
    - user: root
    - group: glance
    - mode: 640
    - template: jinja
    - defaults:
      MYSQL_IP: {{ pillar['mysql']['MYSQL_IP'] }}
      GLANCE_DB_PASS: {{ pillar['glance']['GLANCE_DB_PASS'] }}
      GLANCE_DB_USER: {{ pillar['glance']['GLANCE_DB_USER'] }}
      GLANCE_DB_NAME: {{ pillar['glance']['GLANCE_DB_NAME'] }}
      KEYSTONE_IP: {{ pillar['keystone']['KEYSTONE_IP'] }}
      MEMCACHED_IP: {{ pillar['memcached']['MEMCACHED_IP'] }}
      GLANCE_KEYSTONE_USER: {{ pillar['glance']['GLANCE_KEYSTONE_USER'] }}
      GLANCE_KEYSTONE_PASS: {{ pillar['glance']['GLANCE_KEYSTONE_PASS'] }}
    - require:
      - pkg: glance-pkg

glance-registry-conf:
  file.managed:
    - name: /etc/glance/glance-registry.conf
    - source: salt://openstack/queen/glance/files/glance-registry.conf
    - user: root
    - group: glance
    - mode: 640
    - template: jinja
    - defaults:
      MYSQL_IP: {{ pillar['mysql']['MYSQL_IP'] }}
      GLANCE_DB_PASS: {{ pillar['glance']['GLANCE_DB_PASS'] }}
      GLANCE_DB_USER: {{ pillar['glance']['GLANCE_DB_USER'] }}
      GLANCE_DB_NAME: {{ pillar['glance']['GLANCE_DB_NAME'] }}
      KEYSTONE_IP: {{ pillar['keystone']['KEYSTONE_IP'] }}
      MEMCACHED_IP: {{ pillar['memcached']['MEMCACHED_IP'] }}
      GLANCE_KEYSTONE_USER: {{ pillar['glance']['GLANCE_KEYSTONE_USER'] }}
      GLANCE_KEYSTONE_PASS: {{ pillar['glance']['GLANCE_KEYSTONE_PASS'] }}
    - require:
      - pkg: glance-pkg

glance-db-sync:
  cmd.run:
    - name: su -s /bin/sh -c "glance-manage db_sync" glance && touch /etc/lock/glance-db-sync.lock
    - require:
      - file: glance-api-conf
      - file: glance-registry-conf
    - unless : test -f /etc/lock/glance-db-sync.lock


glance-api-service:
  service.running:
    - name: openstack-glance-api
    - enable: True
    - require:
      - cmd: glance-db-sync
    - watch:
      - file: glance-api-conf

glance-registry-service:
  service.running:
    - name: openstack-glance-registry
    - enable: True
    - require:
      - cmd: glance-db-sync
    - watch:
      - file: glance-registry-conf

glance-service-endpoint:
  file.managed:
    - name: /etc/script/glance-service-endpoint.sh
    - source: salt://openstack/queen/glance/files/glance-service-endpoint.sh
    - user: root
    - group: root
    - mode: 700
    - template: jinja
    - defaults:
      GLANCE_IP: {{ pillar['glance']['GLANCE_IP'] }}
  cmd.run:
    - name: bash /etc/script/glance-service-endpoint.sh && touch /etc/lock/glance-service-endpoint.lock
    - require:
      - service: glance-api-service
      - service: glance-registry-service
    - unless : test -f /etc/lock/glance-service-endpoint.lock
